<?php

class Catalogue_Helper_Application extends Workset_Controller_Action_Helper_Application {

	function getCategory($id, $req = false) {
        $request = $this->getRequest();

		if (true == $req) {
			$id = (int)$request->getParam((string)$id);
		} else {
			$id = (int)$id;
		}

		if ($id < 1) {
            throw $this->throwException(null, 404);
        }

        $category = Catalogue_Model_Category::getInstance($id);

        if (!$category) {
            throw $this->throwException(null, 404);
        }

		return $category;
	}

    function getObject($id, $req = false) {
        $request = $this->getRequest();

        if (true == $req) {
			$id = (int)$request->getParam((string)$id);
		} else {
			$id = (int)$id;
		}

		if ($id < 1) {
            throw $this->throwException(null, 404);
        }

        $object = Catalogue_Model_Object::getInstance($id);

        if (!$object) {
            throw $this->throwException(null, 404);
        }

		return $object;
    }

    function getAttribute($id, $req = false, $enum = false) {

        $request = $this->getRequest();

        if (true == $req) {
			$id = (int)$request->getParam((string)$id);
		} else {
			$id = (int)$id;
		}

		if ($id < 1) {
            throw $this->throwException(null, 404);
        }

        if (false == $enum) {
            $attr = Catalogue_Model_Attribute::getInstance($id);
        } else {
            $attr = Catalogue_Model_Attribute_Enum::getInstance($id);
        }

        if (!$attr) {
            throw $this->throwException(null, 404);
        }

		return $attr;
    }

    function getAttrItem($id, $req = false) {
        $request = $this->getRequest();

        if (true == $req) {
			$id = (int)$request->getParam((string)$id);
		} else {
			$id = (int)$id;
		}

		if ($id < 1) {
            throw $this->throwException(null, 404);
        }

        $item = Catalogue_Model_Attribute_Enum_Item::getInstance($id);

        if (!$item) {
            throw $this->throwException(null, 404);
        }

		return $item;
    }

	function getDefaultCategories(Catalogue_Model_Category $category) {
		$result = array();

        $actionConfig = $this->getActionController()->getActionConfig();

		if (!empty($actionConfig['default_category_set'])) {
			$codes = null; $key = null;
			if (isset($actionConfig['default_category_set'][$category['code']])) {
				$codes = (array)$actionConfig['default_category_set'][$category['code']];
				$key = 'code';
			} elseif (isset($actionConfig['default_category_set'][$category['id']])) {
				$codes = (array)$actionConfig['default_category_set'][$category['id']];
				$key = 'id';
			}

			if (null === $codes) {
				while ($category->hasParent()) {
					$category = $category->getParent();
					$result = $this->getDefaultCategories($category);
					if ($result) {
						break;
					}
				}
			} elseif ($codes) {
				$result = Catalogue_Model_Category::getAll(
                    array(
                        "$key in(?)" => $codes
                    ),
                    array('sort ASC', 'id DESC')
                )->setKeyIndex('id')->toArray();
			}
		}

		return $result;
	}

}